<?php
/**
 *	@package WordPress
 *	@subpackage Grid_Focus
 */
if ( function_exists('register_sidebar') ) {
	register_sidebar(array(
		'name' => 'Primary - Kairos1',
		'before_widget' => '<div id="%1$s" class="widgetContainer %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h3 class="widgetTitle">',
		'after_title' => '</h3>'
	));
	register_sidebar(array(
		'name' => 'Primary - Kairos2',
		'before_widget' => '<div id="%1$s" class="widgetContainer %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h3 class="widgetTitle">',
		'after_title' => '</h3>'
	));
	register_sidebar(array(
		'name' => 'Secondary - Shared',
		'before_widget' => '<div id="%1$s" class="widgetContainer %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h3 class="widgetTitle">',
		'after_title' => '</h3>'
	));
	register_sidebar(array(
		'name' => 'Primary - Kairos3',
		'before_widget' => '<div id="%1$s" class="widgetContainer %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h3 class="widgetTitle">',
		'after_title' => '</h3>'
	));
	register_sidebar(array(
		'name' => 'Primary - Kairos4',
		'before_widget' => '<div id="%1$s" class="widgetContainer %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h3 class="widgetTitle">',
		'after_title' => '</h3>'
	));
	register_sidebar(array(
		'name' => 'Third Column',
		'before_widget' => '<div id="%1$s" class="widgetContainer %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h2 class="widgetTitle">',
		'after_title' => '</h2>'
	));
} 

add_filter('comments_template', 'legacy_comments');
	function legacy_comments($file) {
	if(!function_exists('wp_list_comments')) : // WP 2.7-only check
	$file = TEMPLATEPATH . '/legacy.comments.php';
	endif;
	return $file;
}

// custom functions
function image($url, $alt='') {
	return '<img src="' . $url . '" alt="' . $alt . '" />';
}

function linkto($name, $link = '#') {
	return '<a href="' . $link . '">' . $name . '</a>';
}

function linktoimage($imgurl, $link, $alt='') {
	return linkto(image($imgurl, $alt), $link);
}

function featured_posts($titletag = 'h2') {
	echo '<'. $titletag . '>Featured Posts &amp; Events</' . $titletag . '>';
	query_posts('cat=30'); 

	// the Loop
	while (have_posts()) : the_post();
	?>
	
	<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
	
	<?php
		$thumbnail = get_post_custom_values('thumbnail');
		echo '<div class="featured">';

		if ($thumbnail) {
			echo linktoimage($thumbnail[0], get_permalink());
		}

		echo '</div> <!-- end featured -->';

	endwhile;
}

// similar to sidebar_recent_posts(), but doesn't care about subgroupings
function sidebar_recent_sideposts() {
	query_posts('cat=-3&showposts=8&caller_get_posts=1'); // exclude "MAIN" categories
	while (have_posts()) : the_post(); 
			
		// 432000 == 5 days
		$new = ((time() - get_the_time('U')) <  432000) ? '<span class="new">NEW</span>' : '';
		// $new = '';
	?>
		<div class="boxgrid caption">
	<?php
		// show thumbnail
		$thumbnail = get_post_custom_values('thumbnail');
		if ($thumbnail) {
			echo linktoimage($thumbnail[0], get_permalink());
		}
		// need to have a replacement thumbnail here
	?>				
			<div class="cover boxcaption">
				<h4><?php echo $new ?><a href="<?php echo get_permalink() ?>"><?php echo get_the_title() ?></a></h4>
				<p> <?php echo substr(get_the_excerpt(), 0, 120) . ' ' . linkto('[...]', get_permalink()); ?> </p>
			</div>
		</div> <!-- end .boxgrid -->
	
		<div class="postMeta">
			<p class="container-left">
				<span class="author"><?php the_author() ?></span>
			</p>
			<p class="container">
				<span class="date"><?php the_time('M j') ?></span>
				<span class="comments"><?php comments_popup_link('0', '1', '%'); ?></span>
			</p>
		</div>

	<?php
	endwhile;		
}

function sidebar_albums() {
	$display = '';
	query_posts('cat=72&showposts=-1&caller_get_posts=1'); // grab from category "photoset"
	
	while (have_posts()) : the_post(); 
		$thumbnail = get_post_custom_values('thumbnail');
		
		if ($thumbnail) {
			$thumbnail = $thumbnail[0];
			if (strpos($thumbnail, 'flickr')) {
				// $thumbnail should be smtg like http://farm3.static.flickr.com/2584/3926130749_52dd4b2f5b_m.jpg
				$thumbnail = substr($thumbnail, 0, -5) . 't.jpg';	// change _m to _t
			}
		}

		$display .= '<p>' . linktoimage($thumbnail, get_permalink(), get_the_title()) . '</p>';

	endwhile;
	
	echo $display;
}

// sort by recency & homegroups 
function sidebar_recent_posts() {
	$list = array(4 => 'kairos1', 5 => 'kairos2', 10 => 'kairos4');
	$postarray = array();
	
	foreach ($list as $id => $group) {
		query_posts('cat='.$id.'&showposts=2'); 
		// the Loop
		while (have_posts()) : the_post(); 
			$postarray[] = array('group' => $group, 'link' => get_permalink(), 'title' => get_the_title(), 'excerpt' => get_the_excerpt(), 'date' => get_the_time('U'), 'comments' => get_comments_number(), 'thumbnail' => get_post_custom_values('thumbnail'));

		endwhile;
	} // end foreach()
	
	// sorting (by date, most recent first)
	usort($postarray, 'compare_date');
	// print_r($postarray);
	
	foreach ($postarray as $post) {
		$comments_link = $post['link'];
		$comments_link .= ($post['comments']) ? '#comments' : '#respond';
		switch ($post['group']) {
			case 'kairos1': $groupname = 'Rick&amp;Sue'; break;
			case 'kairos2': $groupname = 'Andy&amp;Amy'; break;
			case 'kairos4': $groupname = 'Steve&amp;Eunice'; break;
			default: $groupname = ''; break;
		}
		// 432000 == 5 days
		$new = ((time() - $post['date']) <  432000) ? '<span class="new">NEW</span>' : '';
		// $new = '';
		?>
			<h4><?php echo $new ?><a href="<?php echo $post['link'] ?>"><?php echo $post['title'] ?></a></h4>
			<table border="0" cellspacing="0" cellpadding="0" width="100%">
				<tr>
					<td class="groupname"><?php echo $groupname?></td>
					<td class="groupMeta"><span class="date"><?php echo date('M j', $post['date']); ?></span> <span class="comments"><a href="<?php echo $comments_link;?>"><?php echo $post['comments']; ?></a></span></td>
				</tr>
			</table>

		<?php
			// show thumbnail
			$thumbnail = $post['thumbnail'];
			if ($thumbnail) {
				echo linktoimage($thumbnail[0], $post['link']);
			}
			
			// post
			echo substr($post['excerpt'], 0, 120) . ' ' . linkto('[...]', $post['link']);
			
	} // end foreach()	
}

// to be used within THE LOOP
function selective_categories() {
	$exclude_list = array(3, 4, 5, 9, 10);
	foreach((get_the_category()) as $category) { 
		$id = $category->cat_ID;
		if (!in_array($id, $exclude_list)) {
			echo linkto($category->cat_name, get_category_link($id)) . ' '; 
		} 
	} 	
}

// found via: http://www.the-art-of-web.com/php/sortarray/
function compare_date($a, $b) { 
	$cmp = 0;
	if ($a['date'] > $b['date']) $cmp = -1;
	else if ($a['date'] < $b['date']) $cmp = 1;
	return $cmp;
}
?>